package cz.cvut.fit.mi_paa.bucket.strategy;

import cz.cvut.fit.mi_paa.abs.Context;
import cz.cvut.fit.mi_paa.bucket.BucketInstance;

import java.util.PriorityQueue;
import java.util.Queue;

/**
 * Created with IntelliJ IDEA.
 * User: milos
 * Date: 20.11.12
 * Time: 8:43
 * To change this template use File | Settings | File Templates.
 */
public class EuclidBucketStrategy extends BucketStrategy {
    public EuclidBucketStrategy(Context context) {
        super(context);
    }

    @Override
    public Queue getQueue() {
        return new PriorityQueue(QUEUE_LENGTH,new BucketComparator(){

            @Override
            public int compare(BucketInstance o1, BucketInstance o2) {
                return Double.compare(o1.getScore(), o2.getScore());
            }
        });
    }

    @Override
    public BucketScoreGenerator getScoreGenerator() {
        return new BucketScoreGenerator() {
            @Override
            public double getScore(BucketInstance instance) {
                double score = 0;
                for (int i=0;i<instance.getNumBuckets();i++) {
                    score += Math.pow(instance.getEndConfiguration()[i] - instance.getBuckets()[i].getVolume(),2);
                }
                return Math.sqrt(score);
            }
        };
    }
}
